import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { keyword } from '../../keyword.data';
import { appKeywordUrl } from '../../keyword.url';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { InitService } from '../../../public/serve/init/init.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
declare var $:any;

@Component({
  selector: 'app-trial-synonym',
  templateUrl: './trial-synonym.component.html',
  styleUrls: ['./trial-synonym.component.css']
})
export class TrialSynonymComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();

  total      : number = 0;
  naturalNum : number = 0;

  tabDetailVal : Array<object>;
  details: object = {};
  @Input() synonymName :any = '';

  isDisabled : boolean = true;
  themeWordsId :any;
  groupId : any;
  twId : any;
  id : any;

  constructor(
    private server: Service,
    public request: CudTableService,
    private tableListService: CudTableService,
    private hintsService: HintsService,
    private removeTableService: RemoveTableService,
    public initService: InitService
  ) { 
    
  }

  ngOnInit() {
  }

  /**
   * 详情
   */
  _details(info: object){
    this.details = info;
    this.themeWordsId = info['themeWordsId'];
    this.getSynonymGroupList();
  }

  /**
   * 同义词组列表查询列表
   */
  getSynonymGroupList() {
    let data = this._tableSynonymData();
    this.request.postRequest(this.server.baseUrl +appKeywordUrl.getSynonymGroupList, data).then((res: any) => {
      if (res.result_code === '0000') {
        this.tabDetailVal = res.result;
        this.total = res.length;
        this.isSetMainSynony();
      } else {
        this.hintsService.messages(res.message, res.result_code);
      }
      return;
    })
      .catch((err: any) => {
        this.hintsService.messages("error", "error");
        return;
      });
  }

  /**
   * 查询是否已经设置主同义词
   */
  isSetMainSynony(){
    if(this.tabDetailVal){
      for (let val of this.tabDetailVal) {
        if(val['status'] == '1'){
          this.isDisabled = false;
          this.id = val['id'];
          this.groupId = val['groupId'];
          this.twId = val['themeWordsId'];
        }
      }
    }
   
  }

  //查询同义词参数
  private _tableSynonymData() {
    return {
      token: keyword.retrieval.token,
      pageNo: keyword.pagination.pageNo,
      pageSize: keyword.pagination.pageSize,
      themeWordsId: this.themeWordsId,
    }
  }

   // 单选（多选）
  _chose(val) {
    this.isDisabled = false;
    this.id = val.id;
    this.groupId = val.groupId;
    this.twId = val.themeWordsId;
  }

  /**
   * 设为同义词
   */
  _setMainSynonyStatus(){
    let url = this.server.baseUrl + appKeywordUrl.setMainSynonyStatus;
    let data = { 
      token: keyword.retrieval.token, 
      id: this.id,
      groupId: this.groupId,
      themeWordsId: this.twId
    };
    this.request
      .postRequest(url, data)
      .then(res => {
        if (res.result_code === "0000") {
            this.onSubmit.emit();
        }
        this.hintsService.messages(res.message, res.result_code);
        this._initAdd();
        return;
      })
      .catch(err => {
        this.hintsService.messages("error", "error");
        this._initAdd();
        return;
      });
  }

  //删除
  ids = [];
  onDeleteConfirm(id){
    this.ids.push(id);
  }
  /**
   * 删除同义词组
   */
  _deleteSynonymGroup(){
    let url = this.server.baseUrl + appKeywordUrl.deleteSynonymGroup;
    let data = { 
      token: keyword.retrieval.token, 
      ids: this.ids
    };
    this.request
      .postRequest(url, data)
      .then(res => {
        if (res.result_code === "0000") {
            this.getSynonymGroupList();
        }
        this.hintsService.messages(res.message, res.result_code);
        this._clearId();
        return;
      })
      .catch(err => {
        this.hintsService.messages("error", "error");
        this._clearId();
        return;
      });
  }

  _clearId(){
    this.ids = [];
  }

  /**
    * 初始化
    */
   private _initAdd(){
    this.isDisabled = true;
    this.id = '';
    this.groupId = '';
    this.twId = '';
    this.themeWordsId = '';
    this.initService._init();
    $("#synonymManagementDetals").modal("hide");
   }

}
